package com.luohai.langchain4jspringboot.config;

import com.luohai.langchain4jspringboot.aiservice.AiMessagesService;
import com.luohai.langchain4jspringboot.repository.RedisChatMemoryStore;
import dev.langchain4j.memory.ChatMemory;
import dev.langchain4j.memory.chat.ChatMemoryProvider;
import dev.langchain4j.memory.chat.MessageWindowChatMemory;
import dev.langchain4j.model.openai.OpenAiChatModel;
import dev.langchain4j.service.AiServices;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author luoh6
 */
@Configuration
public class CommonConfig {

//    @Autowired
//    private OpenAiChatModel openAiChatModel;

//    @Bean
//    public AiMessagesService aiMessagesService(){
//        AiMessagesService build = AiServices.builder(AiMessagesService.class)
//                .chatModel(openAiChatModel)
//                .build();
//        return build;
//    }

    @Bean(value = "chatMemory")
    public ChatMemory chatMemory(){
        return MessageWindowChatMemory.builder().maxMessages(20).build();
    }

    @Autowired
    private RedisChatMemoryStore redisChatMemoryStore;


    @Bean(value = "chatMemoryProvider")
    public ChatMemoryProvider chatMemoryProvider(){
        return new ChatMemoryProvider() {
            @Override
            public ChatMemory get(Object memoryId) {
                return MessageWindowChatMemory.builder()
                        .maxMessages(20)
                        .id(memoryId)
                        .chatMemoryStore(redisChatMemoryStore)
                        .build();
            }
        };
    }
}
